//=============================================================================

#include <math.h>
#include <windows.h>
#include "../../../../inc/aCamera.h"

//=============================================================================

aCamera::aCamera()
{
}

//=============================================================================

void aCamera::acSetCamera(const aVector& vec, const aVector& vec2, const aVector& vec3)
{
  position.x = vec.x;
  position.y = vec.y;
  position.z = vec.z;

  viewPoint.x = vec2.x;
  viewPoint.y = vec2.y;
  viewPoint.z = vec2.z;

  upVector.x = vec3.x;
  upVector.y = vec3.y;
  upVector.z = vec3.z;
}

//=============================================================================

void aCamera::acMoveCamera(float speed)
{
  aVector tmp = AUX.aaNormalize(viewPoint - position)*speed;

  position += tmp;
  viewPoint += tmp;
}

//=============================================================================

void aCamera::acRotateCamera(float angle, float x, float y, float z)
{
  aVector vec, vec2;
  float mCos = cos(angle);
  float mSin = sin(angle);
  float mmCos = 1 - mCos;

  vec.x = viewPoint.x - position.x;
  vec.y = viewPoint.y - position.y;
  vec.z = viewPoint.z - position.z;

  vec2.x =  (mCos + mmCos*x*x)*vec.x;
  vec2.x += (mmCos*x*y - z*mSin)*vec.y;
  vec2.x += (mmCos*x*z + y*mSin)*vec.z;

  vec2.y =  (mmCos*x*y + z*mSin)*vec.x;
  vec2.y += (mCos + mmCos*y*y)*vec.y;
  vec2.y += (mmCos*y*z - x*mSin)*vec.z;

  vec2.z =  (mmCos*x*z - y*mSin)*vec.x;
  vec2.z += (mmCos*y*z + x*mSin)*vec.y;
  vec2.z += (mCos + mmCos*z*z)*vec.z;

  viewPoint.x = position.x + vec2.x;
  viewPoint.y = position.y + vec2.y;
  viewPoint.z = position.z + vec2.z;
}

//=============================================================================

void aCamera::acCheckMouse(int x, int y)
{
  POINT mp;
  static float currentRot = 0.0;
  aVector vec;

  GetCursorPos(&mp);

  if (mp.x == x && mp.y == y)
    return;

  float angleY = (float)((x - mp.x))/1000.0f;
  float angleZ = (float)((y - mp.y))/1000.0f;

  currentRot -= angleZ;
  vec = AUX.aaCrossMulty(viewPoint - position, upVector);
  vec = AUX.aaNormalize(vec);
  
  if (currentRot > 0.98f)
    currentRot = 0.98f;
  else
    if (currentRot < -0.98f)
      currentRot = -0.98f;
    else
    {
      acRotateCamera(angleZ, vec.x, vec.y, vec.z);
      acRotateCamera(angleY, 0.0f, 1.0f, 0.0f);
    }
  //SetCursorPos(x, y);
}

//=============================================================================

void aCamera::acUpdateCamera(int x, int y)
{
  strafeVector = AUX.aaNormalize(aAux::aaGetInstance().aaCrossMulty(viewPoint - position, upVector));
  acCheckMouse(x, y);
}

//=============================================================================

void aCamera::acStrafeCamera(float speed)
{
  strafeVector = AUX.aaNormalize(AUX.aaCrossMulty(viewPoint - position, upVector));

  position.x += strafeVector.x*speed;
  position.z += strafeVector.z*speed;

  viewPoint.x += strafeVector.x*speed;
  viewPoint.z += strafeVector.z*speed;
}

//=============================================================================

void aCamera::acMoveUp(float speed)
{
  position.y += speed;
  viewPoint.y += speed;
}

//=============================================================================

aCamera::~aCamera()
{
}

//=============================================================================
